package com.roads.plan.controller;

import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.page.TableDataInfo;
import com.roads.common.enums.BusinessType;
import com.roads.common.utils.SecurityUtils;
import com.roads.plan.domain.PlanProfileScheduleDetail;
import com.roads.plan.domain.PlanProfileScheduleMaster;
import com.roads.plan.service.IPlanProfileScheduleService;
import com.roads.workflow.domain.WorkflowTaskDelivery;
import com.roads.workflow.domain.WorkflowTaskNode;
import com.roads.workflow.service.IWorkflowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 形象进度Controller
 *
 * @author nhp
 * @date 2024-03-05
 */
@RestController
@RequestMapping("/plan/profile/schedule")
public class PlanProfileScheduleController extends BaseController {

    @Autowired
    IPlanProfileScheduleService service;

    @Autowired
    private IWorkflowTaskService taskService;

    /**
     * 获取主表详细信息
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getMasterInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(service.getMasterById(id));
    }

    /**
     * 获取明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetailInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(service.getDetailById(id));
    }

    /**
     * 查询主表列表
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:list')")
    @GetMapping("/list")
    public TableDataInfo masterList(PlanProfileScheduleMaster model) {
        startPage();
        // createBy 设置成当前登录用户，根据用户标段查询数据，排除admin
        if (!SecurityUtils.isAdmin(getUserId())) {
            model.setCreateBy(getUsername());
        }
        List<PlanProfileScheduleMaster> list = service.selectMasterList(model);
        return getDataTable(list);
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:add')")
    @Log(title = "形象进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlanProfileScheduleMaster model) {
        return toAjax(service.insert(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:remove')")
    @Log(title = "形象进度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 获取用户节点列表
     *
     * @param masterId 主键ID
     * @return 结果
     */
    @GetMapping(value = "/selectUserOptions/{masterId}")
    public AjaxResult selectUserOptions(@PathVariable("masterId") Integer masterId) {
        AjaxResult result = new AjaxResult();
        PlanProfileScheduleMaster master = service.getMasterById(masterId);
        List<WorkflowTaskNode> nodes = taskService.selectUserOptions(master.getUuid());
        result.put("currentUser", master.getCurrentUser());
        result.put("nodes", nodes);
        return result;
    }

    /**
     * 传送
     *
     * @param delivery 传送对象
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:delivery')")
    @Log(title = "形象进度", businessType = BusinessType.DELIVERY)
    @PostMapping("/delivery")
    public AjaxResult delivery(@RequestBody WorkflowTaskDelivery delivery) {
        return toAjax(service.delivery(delivery));
    }

    /**
     * 锁定
     *
     * @param ids 主键集合
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:lock')")
    @Log(title = "形象进度", businessType = BusinessType.LOCK)
    @GetMapping("/lock/{ids}")
    public AjaxResult lock(@PathVariable Integer[] ids) {
        return toAjax(service.lock(ids));
    }

    /**
     * 解锁
     *
     * @param masterId 主键
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:unlock')")
    @Log(title = "形象进度", businessType = BusinessType.UNLOCK)
    @GetMapping(value = "/unlock/{masterId}")
    public AjaxResult unlock(@PathVariable("masterId") Integer masterId) {
        ////////// 解锁限制条件
        PlanProfileScheduleMaster master = service.getMasterById(masterId);
        return toAjax(service.unlock(master, masterId));
    }

    /**
     * 查询明细列表
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:list')")
    @GetMapping("/detail/list")
    public AjaxResult detailList(PlanProfileScheduleDetail model) {
        List<PlanProfileScheduleDetail> list = service.selectDetailList(model);
        return AjaxResult.success(list);
    }

    /**
     * 修改月形象计划明细
     */
    @PreAuthorize("@ss.hasPermi('plan:profile:schedule:detail:edit')")
    @Log(title = "形象进度-明细", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public AjaxResult edit(@RequestBody PlanProfileScheduleDetail model) {
        model.setUpdateBy(getUsername());
        return toAjax(service.updateDetail(model));
    }

}
